/*
 * @Descripttion: your project
 * @version: 1.0
 * @Author: hongweixun
 * @Date: 2022-06-28 18:06:09
 * @LastEditors: hongweixun
 * @LastEditTime: 2022-09-30 10:15:14
 */

import { createStore, createLogger } from "vuex";
import { getFacullyApi, getGradeApi, getClassApi, getWorkerApi, Params, getTreeForClass } from '../components/Options/SchoolQues'
import { Toast } from 'vant'
export default createStore({
    state: {
        // role_type: 3 // 1 管理员 2 年级主任 3班主任 5宿舍主管 6宿管员
        // roletype 10 班主任 12 级长 其它按学部
        loginForm: window.sessionStorage.getItem('loginForm') ? JSON.parse(window.sessionStorage.getItem('loginForm') as any) : {},
        // loginForm: {
        // },
        active: '1',
        facully: [],
        grade: [],
        class: [],
        workerList: [],
        sessionList: []
    },
    mutations: {
        SET_LOGINFORM(store, val) {
            store.loginForm = val
        },
        SET_ACTIVE(store, val) {
            store.active = val
        },
        SET_FACULLY(store, val) {
            store.facully = val
        },
        SET_GRADE(store, val) {
            store.grade = val
        },
        SET_CLASS(store, val) {
            store.class = val
        },
        SET_SESSION(store, val) {
            store.sessionList = val
        },
        SET_WORKER(store, val) {
            store.workerList = val
        }
    },
    actions: {
        setLoginForm(context, obj) {
            context.commit('SET_LOGINFORM', obj)
        },
        setActive(context, obj) {
            context.commit('SET_ACTIVE', obj)
        },
        // 获取课节
        setSession(context, obj) {
            context.commit('SET_SESSION', obj)
        },
        // 获取班级的层级树结构
        async setClassTree(context) {
            const params = {
                type: 'getClassTree',
                type_all: 'type_all'
            }
            const res:any = await getTreeForClass(params)
            if(res.code !== 1){
                return Toast(res.msg)
            }
            console.log(res.data)
            context.commit('SET_FACULLY', res.data)
        },
        // 获取学部
        async setFacully(context, obj) {
            console.log(obj)
            const params: Params = {
                type: ''
            }
            if (obj === 1) {
                params.type = obj
            }
            const res: any = await getFacullyApi(params)
            if (res.code !== 1) {
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: { faculty_status: any; id: any; faculty_name: any; }) => {
                    if (obj === 1) {
                        const params = {
                            id: item.id,
                            name: item.faculty_name,
                            children: []
                        }
                        str.push(params)
                    } else {
                        if (Number(item.faculty_status) === 1) {
                            const params = {
                                id: item.id,
                                name: item.faculty_name,
                                children: []
                            }
                            str.push(params)
                        }
                    }
                })
            }
            context.commit('SET_FACULLY', str)
        },
        // 获取年级
        async setGrade(context, obj) {
            const params: Params = {
                page: 1,
                page_size: 100
            }
            let res: any = null
            if (obj.facully_id) {
                params.faculty_id = obj.facully_id
                res = await getGradeApi(params)
            } else {
                params.id = obj
                params.type = '2'
                res = await getFacullyApi(params)
            }
            // const params: Params = {
            //     id: obj,
            //     type: '2'
            // }
            // const res: any = await getFacullyApi(params)
            if (res.code !== 1) {
                context.commit('SET_GRADE', [])
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: { grade_status: any; id: any; grade_name: any; }) => {
                    if (item.grade_status && Number(item.grade_status) === 1) {
                        const params = {
                            id: item.id,
                            name: item.grade_name,
                            children: []
                        }
                        str.push(params)
                    } else {
                        const params = {
                            id: item.id,
                            name: item.grade_name,
                            children: []
                        }
                        str.push(params)
                    }
                })
            }
            context.commit('SET_GRADE', str)
        },
        // 获取班级
        async setClass(context, obj) {
            const params: Params = {
                page: 1,
                page_size: 100
            }
            let res: any = null
            if (obj.grade_id) {
                params.grade_id = obj.grade_id
                res = await getClassApi(params)
            } else {
                params.id = obj
                params.type = '3'
                res = await getFacullyApi(params)
            }
            if (res.code !== 1) {
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data.list && res.data.list.length > 0) {
                res.data.list.forEach((item: { class_status: any; id: any; class_name: any; }) => {
                    if (item.class_status && Number(item.class_status) === 1) {
                        const params = {
                            id: item.id,
                            name: item.class_name
                        }
                        str.push(params)
                    } else {
                        const params = {
                            id: item.id,
                            name: item.class_name
                        }
                        str.push(params)
                    }
                })
            }
            context.commit('SET_CLASS', str)
        },
        // 获取教师列表
        async getTeacher(context, obj) {
            console.log(obj)
            const params: Params = {
                page: 1,
                page_size: 1000
            }
            const res: any = await getWorkerApi(params)
            if (res.data.list && res.data.list.length > 0) {
                res.data.list.forEach((item: { show: boolean; checked: boolean; id: any; }) => {
                    item.show = true
                    item.checked = false
                })
            }
            context.commit('SET_WORKER', res.data.list)
        }
    },
    plugins: import.meta.env.DEV ? [createLogger()] : []
});